package com.tencent.qcloud.timchat.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;
import com.wbao.dianniu.utils.ActivityUtils;
import com.wbao.dianniu.utils.GlideCircleTransform;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 聊天界面adapter
 */
public class ChatAdapter extends ArrayAdapter<Message> {

    private final String TAG = "ChatAdapter";

    private int resourceId;
    private View view;
    private ViewHolder viewHolder;
    private String selfUrl;
    private String toUrl;
    private String identify;

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public long getItemId(int position) {
        return view != null ? view.getId() : position;
    }

    /**
     * Constructor
     *
     * @param context  The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects  The objects to represent in the ListView.
     */
    public ChatAdapter(Context context, int resource, List<Message> objects, String selfUrl, String toUrl, String identify) {
        super(context, resource, objects);
        resourceId = resource;
        this.selfUrl = selfUrl;
        this.toUrl = toUrl;
        this.identify = identify;
    }

    public void updateToUrl(String toUrl) {
        this.toUrl = toUrl;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null) {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.leftMessage = (RelativeLayout) view.findViewById(R.id.leftMessage);
            viewHolder.rightMessage = (RelativeLayout) view.findViewById(R.id.rightMessage);
            viewHolder.leftPanel = (RelativeLayout) view.findViewById(R.id.leftPanel);
            viewHolder.rightPanel = (RelativeLayout) view.findViewById(R.id.rightPanel);
            viewHolder.rightHeadIV = (ImageView) view.findViewById(R.id.rightAvatar);
            viewHolder.leftHeadIV = (ImageView) view.findViewById(R.id.leftAvatar);
            viewHolder.sending = (ProgressBar) view.findViewById(R.id.sending);
            viewHolder.error = (ImageView) view.findViewById(R.id.sendError);
            viewHolder.sender = (TextView) view.findViewById(R.id.sender);
            viewHolder.rightDesc = (TextView) view.findViewById(R.id.rightDesc);
            viewHolder.systemMessage = (TextView) view.findViewById(R.id.systemMessage);
            view.setTag(viewHolder);
        }
        if (position < getCount()) {
            final Message data = getItem(position);
            if (data.isSelf()) {
                displayDefaultHeadImg(getContext(), viewHolder.rightHeadIV, selfUrl);
            } else {
                displayDefaultHeadImg(getContext(), viewHolder.leftHeadIV, toUrl);
            }
            data.showMessage(viewHolder, getContext());
        }
        viewHolder.leftHeadIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != identify) {
                    int accountId = 0;
                    try {
                        identify = identify.replace("qa", "").replace("prod", "");
                        accountId = Integer.valueOf(identify);
                        ActivityUtils.intoHeadInfoActivity(accountId, getContext());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return view;
    }

    private void displayDefaultHeadImg(Context context, ImageView imageView, String imgUrl) {
        if (null != context) {
            Glide.with(context)
                    .load(imgUrl)
                    .centerCrop()
                    .crossFade()
                    .override(Utils.dip2px(context, 35), Utils.dip2px(context, 35))
                    .bitmapTransform(new GlideCircleTransform(context))
                    .thumbnail(0.2f)
                    .into(imageView);
        }
    }

    public class ViewHolder {
        public RelativeLayout leftMessage;
        public RelativeLayout rightMessage;
        public RelativeLayout leftPanel;
        public RelativeLayout rightPanel;
        public ProgressBar sending;
        public ImageView error;
        public TextView sender;
        public TextView systemMessage;
        public TextView rightDesc;
        public ImageView rightHeadIV;
        public ImageView leftHeadIV;
    }
}
